package com.joyer.web.aop;

import com.joyer.core.valid.InterfaceUtil;
import com.joyer.core.valid.ValidatedStatusEntry;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;

/**
 * User: zhiweifan
 * Date: 2014/10/13
 * Time: 13:27
 */
@Aspect
@Component
public class ControllerValidAOP {
    @Autowired
    private InterfaceUtil appInterfaceUtil;

    /**
     * 被拦截方法须符合验证规则 @Valid Object obj, BindingResult br
     * @param point
     * @return
     * @throws Throwable
     */
 //   @Around("execution(* com.controller.*Controller.*Valid(..))||execution(* com.web.controller.DealerController.*Valid(..))")
    @Around("execution(* cn.com.cmt.media.controller.*Controller.*Valid(..))||execution(* cn.com.cmt.media.controller.wx.*Controller.*Valid(..))||execution(* cn.com.cmt.media.controller.app.*Controller.*Valid(..))")
    public Object validateParams(ProceedingJoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        if(args[1] != null && (args[1] instanceof BindingResult)) {
            ValidatedStatusEntry dto = appInterfaceUtil.getParamsValidatedStatus((BindingResult)args[1]);
            if (dto.isHasErrors()) {
                return dto.getResult();
            }
        }
        Object obj =  point.proceed();
        return obj;
    }

    @AfterReturning("execution(* cn.com.cmt.media.controller.*Controller.*Test(..))")
    public void validateParams1() throws Throwable {
        System.out.printf("测试aop异常");
    }
}
